package cn.iocoder.yudao.module.trade.controller.app.lifecircle;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.lifecircle.vo.LifeCircleStorePageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.lifecircle.vo.LifeCircleStoreRespVO;
import cn.iocoder.yudao.module.trade.controller.app.lifecircle.vo.AppLifeCircleStoreRespVO;
import cn.iocoder.yudao.module.trade.controller.app.lifecircle.vo.AppLifeCircleStoreTypeRespVO;
import cn.iocoder.yudao.module.trade.convert.lifecircle.LifeCircleStoreConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.lifecircle.LifeCircleStoreDO;
import cn.iocoder.yudao.module.trade.service.lifecircle.LifeCircleStoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 生活圈-店铺")
@RestController
@RequestMapping("/trade/life-c-circle/store")
@Validated
public class AppLifeCircleStoreController {

    @Resource
    private LifeCircleStoreService lifeCircleStoreService;

    @GetMapping("/list")
    @Operation(summary = "获得店铺列表")
    @Parameters({
            @Parameter(name = "latitude", description = "精度", example = "110"),
            @Parameter(name = "longitude", description = "纬度", example = "120")
    })
    @PermitAll
    public CommonResult<List<AppLifeCircleStoreRespVO>> getLifeCircleStoreList(
            @RequestParam(value = "latitude", required = false) Double latitude,
            @RequestParam(value = "longitude", required = false) Double longitude) {
        List<AppLifeCircleStoreRespVO> res = lifeCircleStoreService.getLifeCircleStoreListApp(latitude, longitude);
        res.sort(Comparator.comparingDouble(AppLifeCircleStoreRespVO::getDistance));
        return success(res);
    }

    @GetMapping("/listAll")
    @Operation(summary = "获得店铺列表")
    @PermitAll
    public CommonResult<List<AppLifeCircleStoreRespVO>> getLifeCircleStoreListAll() {
        List<AppLifeCircleStoreRespVO> res = lifeCircleStoreService.getLifeCircleStoreListApp(null, null);
        res.sort(Comparator.comparingDouble(AppLifeCircleStoreRespVO::getId).reversed());
        return success(res);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺")
    @Parameter(name = "id", description = "门店编号")
    @PermitAll
    public CommonResult<AppLifeCircleStoreRespVO> getOrder(@RequestParam("id") Long id) {
        LifeCircleStoreDO store = lifeCircleStoreService.getLifeCircleStore(id);
        AppLifeCircleStoreRespVO lifeCircleStoreRespVO = LifeCircleStoreConvert.INSTANCE.convert03(store);
        lifeCircleStoreRespVO.setDetailImage(store.getDetailImage());
        List<Long> pointsIds = store.getPointsIds();
        if (!CollectionUtils.isEmpty(pointsIds)) {
            lifeCircleStoreRespVO.setPoints(lifeCircleStoreService.getPointsListByIds(pointsIds));
        } else {
            lifeCircleStoreRespVO.setPoints(new ArrayList<>());
        }
        lifeCircleStoreRespVO.setOpeningTime(store.getOpeningTime());
        lifeCircleStoreRespVO.setClosingTime(store.getClosingTime());
        return success(lifeCircleStoreRespVO);
    }

    @GetMapping("/getTypeList")
    @Operation(summary = "获得店铺分类列表")
    @PermitAll
    public CommonResult<List<AppLifeCircleStoreTypeRespVO>> getTypeList() {
        return success(lifeCircleStoreService.getTypeList());
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺分页")
    @PermitAll
    public CommonResult<PageResult<LifeCircleStoreRespVO>> getLifeCircleStorePage(@Valid LifeCircleStorePageReqVO pageVO) {
        PageResult<LifeCircleStoreRespVO> pageResult = lifeCircleStoreService.getLifeCircleStorePage(pageVO);
        return success(pageResult);
    }

}